﻿using System.Text;

namespace _6Z字形变换
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string s = "PAYPALISHIRING";
            Console.WriteLine(Solution.Convert(s, 3));
             
        }
    }
    public class Solution
    {
        public static string Convert(string s, int numRows)
        {
            if (numRows == 1)
                return s;
            StringBuilder[] rows = new StringBuilder[numRows];
            for (int i = 0; i < numRows; i++)
                rows[i] = new StringBuilder();
            int curRow = 0;
            int direction = -1;
            foreach (char ch in s)
            {
                rows[curRow].Append(ch);
                if (curRow == 0 || curRow == numRows - 1)
                    direction = -direction;
                curRow += direction;
            }
            StringBuilder result = new StringBuilder();
            foreach (var row in rows)
                result.Append(row.ToString());
            return result.ToString();
        }
    }
}
